<template>
  <!-- 用来做宽高比例 -->
  <div class="icons">
    <swiper :options="swiperOption">
      <swiper-slide v-for="(page,index) of pages" v-bind:key="index">
        <!-- 用来布局小图标 -->
        <div class="icon" v-for="item of page" v-bind:key="item.id">
          <div class="icon-img">
            <img class="icon-img-content" v-bind:src="item.imgUrl" alt />
          </div>
          <p class="icon-describtion">{{item.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>
<script>
export default {
  name: "HomeIcons",
  props: {
    list: Array
  },
  data() {
    return {
      swiperOption: {
        autoplay: false //防止icon页面自动滚动
      }
    };
  },
  computed: {
    pages() {
      const pages = [];
      this.list.forEach((item, index) => {
        const page = Math.floor(index / 8); //向下取整
        if (!pages[page]) {
          pages[page] = [];
        }
        pages[page].push(item);
      });
      return pages;
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';
@import '~styles/mixins.styl';

.icons >>>.swiper-container {
  overflow: hidden;
  height: 0;
  padding-bottom: 50%;
  // 在宽度为100%时，向下撑起的高度为50%。宽高比为2
  // 上下两排自然是50%
}

.icons {
  margin-top: 0.1rem;
}

.icons {
  margin-top: 0.1rem;
}

.icon {
  position: relative;
  overflow: hidden;
  float: left;
  height: 0;
  width: 25%; // 一行4个，宽为25%
  padding-bottom: 25%; // 为了让其为正方形向下撑开与宽一样

  .icon-img {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0.44rem; // 22px的区域放文字
    box-sizing: border-box;
    padding: 0.1rem; // 希望有一个间距，css3语法

    .icon-img-content { // 用来调整图片
      height: 100%;
      display: block; // 图片自动居中
      margin: 0 auto;
    }
  }

  .icon-describtion {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    line-height: 0.44rem;
    height: 0.44rem;
    color: $darkTextColor;
    text-align: center; // 文字居中
    ellipsis();
  }
}
</style>